﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

using DomesticPestLib;
using System.Collections;


namespace DomesticPestProject
{
    public partial class frmPest : System.Web.UI.Page
    {
        DLayer dlayer;
        ArrayList objalst;
        entyPest entypest;
        protected void Page_Load(object sender, EventArgs e)
        {
            //btnCommodity.Enabled = false;
            if (!Page.IsPostBack)
            {
                btnPest.Enabled = false;
                BindDetails();
                clear();
            }

        }

        private void BindDetails()
        {
            //entyinfo = new entyInfo();
            dlayer = new DLayer();
            objalst = new ArrayList();
            objalst = dlayer.selectPest();
            gvPest.DataSource = objalst;
            gvPest.DataBind();
        }

        protected void btnInformation_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmInformation.aspx");
        }

        protected void btnMain_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmMain.aspx");
        }

        protected void btnCommodity_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmCommodity.aspx");
        }

        protected void btnTransaction_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmTransaction.aspx");
        }

        protected void btnPest_Click(object sender, EventArgs e)
        {
           // Response.Redirect("frmPest.aspx");
        }

        protected void btnLctn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmLoc.aspx");
        }

        protected void btnPsDsrbtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPestDistribution.aspx");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ddlCtgy.Visible = false;
            ddlEcoImp.Visible = false;
            ddlKgdm.Visible = false;
            txtCtgy.Visible = true;
            txtEcoImp.Visible = true;
            txtkgdm.Visible = true;
        }

        protected void gvPest_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvPest.EditIndex = -1;
            BindDetails();
        }

        protected void gvPest_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string bcd = gvPest.Rows[e.RowIndex].Cells[2].Text;
            (new DLayer()).DeletePest(bcd);
            BindDetails();
        }

        protected void gvPest_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            entypest = new entyPest();
            TextBox txtByrCd = gvPest.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
            TextBox txtSciNm = gvPest.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
            TextBox txtComNm = gvPest.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
            TextBox txtkgdm = gvPest.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
            TextBox txtPylm = gvPest.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;
            TextBox txtOthrNm = gvPest.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox;
            TextBox txtClsNm = gvPest.Rows[e.RowIndex].Cells[8].Controls[0] as TextBox;
            TextBox txtOrdNm = gvPest.Rows[e.RowIndex].Cells[9].Controls[0] as TextBox;
            TextBox txtFamNm = gvPest.Rows[e.RowIndex].Cells[10].Controls[0] as TextBox;
            TextBox txtCtgy = gvPest.Rows[e.RowIndex].Cells[11].Controls[0] as TextBox;
            TextBox txtAddInf = gvPest.Rows[e.RowIndex].Cells[12].Controls[0] as TextBox;
            TextBox txtEcoImp = gvPest.Rows[e.RowIndex].Cells[13].Controls[0] as TextBox;


            entypest.PBCode=txtByrCd.Text;
            entypest.PSName=txtSciNm.Text;
            entypest.PCName=txtComNm.Text;
            entypest.PKngdm=txtkgdm.Text;
            entypest.PPhylum=txtPylm.Text;
            entypest.PSubPhylum=txtOthrNm.Text;
            entypest.PClass=txtClsNm.Text;
            entypest.POrder=txtOrdNm.Text;
            entypest.PFName=txtFamNm.Text;
            entypest.PCategory=txtCtgy.Text;
            entypest.Painfo=txtAddInf.Text;
            entypest.PEcoImp=txtEcoImp.Text;
            
            DLayer d = new DLayer();
            (new DLayer()).UpdatePest(entypest);
            gvPest.EditIndex = -1;
            BindDetails();
        }

        protected void gvPest_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvPest.EditIndex = e.NewEditIndex;
            BindDetails();
        }

        private void clear()
        {
            txtByrCd.Text = "";
            txtSciNm.Text = "";
            txtComNm.Text = "";
            txtkgdm.Text = "";
            txtPylm.Text = "";
            txtOthrNm.Text = "";
            txtClsNm.Text = "";
            txtOrdNm.Text = "";
            txtFamNm.Text = "";
            txtCtgy.Text = "";
            txtAddInf.Text = "";
            txtEcoImp.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            entypest = new entyPest();
            entypest.PBCode = txtByrCd.Text;
            entypest.PSName = txtSciNm.Text;
            entypest.PCName = txtComNm.Text;
            entypest.PKngdm = txtkgdm.Text;
            entypest.PPhylum = txtPylm.Text;
            entypest.PSubPhylum = txtOthrNm.Text;
            entypest.PClass = txtClsNm.Text;
            entypest.POrder = txtOrdNm.Text;
            entypest.PFName = txtFamNm.Text;
            entypest.PCategory = txtCtgy.Text;
            entypest.Painfo = txtAddInf.Text;
            entypest.PEcoImp = txtEcoImp.Text;
            (new DLayer()).SavePest(entypest);
            BindDetails();
            clear();
            //pnlAdd.Visible = true;
        }
    }
}